<template>
  <div>
    <!-- 面包屑导航 S -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑导航 E -->
    <!-- 主体卡片区域 S -->
    <el-card class="box-card">
      <!-- 搜索框和添加用户按钮区域 S -->
      <el-row :gutter="20">
        <!-- 搜索框区域 S -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model.trim="queryData.query" clearable
            @clear="getGoodList" @blur="getGoodList">
            <el-button slot="append" class="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <!-- 搜索框区域 E -->
        <!-- 添加用户按钮区域 S -->
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/update/0')">添加商品</el-button>
        </el-col>
        <!-- 添加用户按钮区域 E -->
      </el-row>
      <!-- 搜索框和添加用户按钮区域 E -->
      <template>
        <!-- 用户列表 S -->
        <el-table :data="goodList" border style="width: 100%" stripe>
          <!-- 自增序号列 -->
          <el-table-column type="index" label="#">
          </el-table-column>
          <!-- 商品名称列 -->
          <el-table-column prop="goods_name" label="商品名称" width="450">
          </el-table-column>
          <!-- 商品价格列 -->
          <el-table-column prop="goods_price" label="商品价格(元)">
          </el-table-column>
          <!-- 商品重量列 -->
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <!-- 创建时间列 -->
          <el-table-column label="创建时间" width="160">
            <template #default="{row}">
              {{row.add_time * 1000 | formDate('yyyy-MM-dd hh:mm:ss')}}
            </template>
          </el-table-column>
          <!-- 操作按钮列  S -->
          <el-table-column label="操作" width="200">
            <template #default="{row}">
              <!-- 修改商品按钮 -->
              <el-button type="primary" size="mini" icon="el-icon-edit"
                @click="editGood(row.goods_id)">
                编辑
              </el-button>
              <!-- 删除商品按钮 -->
              <el-button type="danger" size="mini" icon="el-icon-delete"
                @click="delGood(row.goods_id)">
                删除
              </el-button>
            </template>
          </el-table-column>
          <!-- 操作按钮列  E -->
        </el-table>
        <!-- 分页器 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryData.pagenum" :page-sizes="[5, 10, 15, 20]"
          :page-size="queryData.pagesize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
        <!-- 用户列表 E -->
      </template>
    </el-card>
    <!-- 主体卡片区域 E -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询商品列表的数据对象
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goodList: [],
      // 商品总条数
      total: 0
    }
  },

  created() {
    // 调用获取商品列表函数
    this.getGoodList()
  },

  methods: {
    // 获取商品列表函数
    async getGoodList() {
      const data = await this.$axios.get('/goods', { params: this.queryData })
      if (data.meta.status !== 200) return this.$message.error('获取商品列表失败！')
      this.goodList = data.data.goods
      this.total = data.data.total
    },
    // 页码改变时触发
    handleCurrentChange(pagenum) {
      this.queryData.pagenum = pagenum
      this.getGoodList()
    },
    // 每页显示条数改变时触发
    handleSizeChange(pagesize) {
      this.queryData.pagesize = pagesize
      this.getGoodList()
    },
    // 编辑商品函数
    editGood(id) {
      this.$router.push('/goods/update/' + id)
    },
    // 删除商品函数
    delGood(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { meta } = await this.$axios.delete(`/goods/${id}`)
          if (meta.status !== 200) return this.$message.error('删除商品失败！')
          this.getGoodList()
          this.$message.success('删除商品成功!')
        })
        .catch(() => {
          this.$message('已取消删除')
        })
    }
  }
}
</script>

<style scoped lang='less'>
.el-table {
  font-size: 12px;
}
</style>
